<template>
	<view class="select-height">
		<nav-bar title="选择项目" @click="uni.navigateBack()" />
		<scroll-view scroll-y :style="{height: 'calc(100vh - ' + containerOutsideHeight + 'px)'}">
			<view class="select-project">
				<view class="search">
					<u-search @search="queryBtn" @change="queryBtn" @clear="blurBtn" v-model="searchText" placeholder="请输入关键字"
						:showAction="false" clearabled height="88upx" />
				</view>

				<view class="project-list">
					<view @click="clickItem(item)" v-for="(item, index) in projectList" :key="index" class="project-item">
						<view>{{item.name}}</view>
					</view>
				</view>
				<u-empty v-if="!projectList.length" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import navBar from "@/components/navBar/navBar.vue";
	import {
		getSystemHeightInfo
	} from "@/utils/common.js";
	import searchCom from "@/components/searchCom.vue"
	import {
		projectManagement
	} from '@/api/comprehensive/comprehensive.js'
	import {
		getProjectListByOverhaul
	} from "@/api/engineeringMaintenance/engineeringMaintenance.js";

	export default {
		data() {
			return {
				// 容器之外的高度
				containerOutsideHeight: 0,
				searchText: undefined,
				projectList: [], //项目数组
			}
		},
		components: {
			searchCom
		},
		onLoad(params) {
			console.log(params.type, '-----')
			this.type = params.type
			if (params && params.type === 'engineer') {
				console.log('工程维保接口')
				this.getEngineerProjectList()
			} else {
				this.getAllProjectList()
			}
			this.initContainerData();
		},
		created() {


			// console.log(uni.getStorageSync("isOwner"), 'id是啥')
		},
		methods: {
			/** 初始化容器数据 */
			initContainerData() {
				const {
					stateHeight,
					bottomHeight,
					navbarHeight
				} = getSystemHeightInfo();

				this.containerOutsideHeight = stateHeight + bottomHeight + navbarHeight;
			},
			//获取综合大修可选项目列表
			getAllProjectList() {
				projectManagement({
					currPage: 1,
					pageSize: 9999,
					params: {
						keyword: this.searchText,
						pmStates: ['zj', 'zt'],
						projectId: uni.getStorageSync("isOwner")

					},
					projectId: uni.getStorageSync("isOwner"),

				}).then(res => {
					if (res.success && res.status == 1) {
						this.projectList = res.result.list
					}
				})
			},

			//获取工程维保可选项目列表
			getEngineerProjectList() {
				getProjectListByOverhaul({
					currPage: 1,
					pageSize: 9999,
					params: {
						// commonKeyword: this.searchText,
						projectStates: ['zj', 'zt'],
						projectId: uni.getStorageSync("isOwner")

					},
					projectId: uni.getStorageSync("isOwner"),

				}).then(res => {
					if (res.success && res.status == 1) {
						this.recordProjectList = res.result.list.map(item => ({
							...item,
							name: item.projectName
						}))
						this.projectList = JSON.parse(JSON.stringify(this.recordProjectList))
					}
				})
			},
			//点击进行传值  在proFillForm里面selectProject方法接收
			clickItem(item) {
				// console.log(item, this.$config, '选择项目页面的')
				this.$$emit(this.$config.ENV_SELECT_PROJECT, item);
				uni.navigateBack({})

			},
			queryBtn(val) {
				this.searchText = val
				if (this.type === 'engineer') {
					this.projectList = JSON.parse(JSON.stringify(this.recordProjectList.filter(item => item.projectName.indexOf(val) !== -1)))
					// this.getEngineerProjectList()
				} else {
					this.getAllProjectList()
				}
			},
			blurBtn(val) {
				this.searchText = ''
				if (this.type === 'engineer') {
					this.projectList = JSON.parse(JSON.stringify(this.recordProjectList.filter(item => item.projectName.indexOf(val) !== -1)))
					// this.getEngineerProjectList()
				} else {
					this.getAllProjectList()
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.select-project {
		font-size: 32upx;

		.search {
			padding: 42upx 36upx 14upx 28upx;
			position: sticky;
			top: 0px;
			border-top: 1px solid #fff;
			background: #fff;
		}

		.project-list {
			padding: 16upx 36upx 0 28upx;
			color: #445469;
			font-weight: 500;

			.project-item {
				padding: 42upx 0 28upx 28upx;
				border-bottom: 1px solid #e2e5ed;
				
				> view {
					display: -webkit-box;
					-webkit-line-clamp: 3;
					/* 显示三行文本 */
					-webkit-box-orient: vertical;
					text-overflow: ellipsis;
					white-space: normal;
					word-wrap: break-word;	
					overflow: hidden;
				}

				&:last-of-type {
					border-bottom: none;
				}
			}
		}
	}
	.select-height {
		/* #ifdef MP-WEIXIN */
			padding-top:190rpx
		/* #endif */
	}
</style>